package mine.auction;

import java.util.ArrayList;
import java.util.List;

/**
 * A class to model an item in an mine.auction.
 */
public class Item {
	
	// A unique identifying number.
	private final int number;

	// A description of the item.
	private final String description;
	
	// The list of bids for this item.
	private final ArrayList<Bid> bids;

	// The current highest bid for this item.
	private Bid highestBid;

	/**
	 * Construct an Item, setting its number and description.
	 * 
	 * @param number
	 *            The item number.
	 * @param description
	 *            A description of this item.
	 */
	public Item(int number, String description) {
		this.number = number;
		this.description = description;
		this.bids = new ArrayList<Bid>();
	}

	/**
	 * Attempt to bid for this item. A successful bid must have a value higher
	 * than any existing bid.
	 * 
	 * @param bid
	 *            A new bid.
	 * @return true if successful, false otherwise
	 */
	public boolean bidFor(Bid bid) {
		if ((highestBid == null) || (bid.getValue() > highestBid.getValue())) {
			// This bid is the best so far.
			setHighestBid(bid);
			bids.add(bid);
			return true;
		} else {
			return false;
		}
	}

	/**
	 * @return The item's number.
	 */
	public int getNumber() {
		return number;
	}

	/**
	 * @return The item's description.
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @return The highest bid for this item. This could be null if there are no
	 *         current bids.
	 */
	public Bid getHighestBid() {
		return highestBid;
	}

	/**
	 * @return The list of bids for this item.
	 */
	public List<Bid> getBids() {
		return this.bids;
	}
	
	/**
	 * @param highestBid
	 *            The new highest bid.
	 */
	private void setHighestBid(Bid highestBid) {
		this.highestBid = highestBid;
	}
}
